package com.isoft.student.controller;

import com.isoft.student.eo.ClassesEOPage;
import com.isoft.student.model.Classes;
import com.isoft.student.service.ClassesService;
import com.isoft.student.tool.ResponseMessage;
import com.isoft.student.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据接口
 */
@Api(tags = "classes")
@RestController
public class ClassesController {

    @Autowired
    private ClassesService classesService;

    @ApiOperation(value = "班级添加方法")
    @RequestMapping(value = "/addClasses",method = RequestMethod.GET)
    public ResponseMessage add(Classes classes){
        int IsAdd = 0;
        //传入数据的判断
        //1.实体对象不能为空
        if (classes!=null){
            //2.答题提交不能为空
            if (classes.getClassname()!=null){
                //3.添加
                IsAdd =classesService.AddClasses(classes);
                if (IsAdd == 1) {
                    return Result.success("保存成功");
                } else if (IsAdd == -1) {
                    return Result.error("角色重复 不允许添加 ");
                } else {
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }

    @ApiOperation(value = "班级修改方法")
    @RequestMapping(value = "/updateClasses",method = RequestMethod.GET)
    public ResponseMessage updateClasses(Classes classes){
        //传入的参数不能为空
        if (classes!=null){
            //参数当中主键不能为0
            if (classes.getClassid()!=0){
                if (classesService.UpdateClasses(classes)==1){
                    if (classes.getClassname()!=null){
                        if (classes.getSchoolname()!=null){

                        }
                    }
                    return Result.success("更新成功");
                }else {
                    return Result.error("更新失败");
                }
            }
        }
        return Result.error("传入的参数不正确");
    }

    @ApiOperation(value = "根据条件获取班级信息")
    @RequestMapping(value = "/getClassesWhereAll",produces = "application/json;charset=utf-8",method=RequestMethod.PUT)
    private ResponseMessage getClassesAll(ClassesEOPage classesEOPage){
        Map<String, List> map=new HashMap<>();
        List<ClassesEOPage> listClasses=classesService.getClassesWhereAll(classesEOPage);
        map.put("list",listClasses);
        return Result.success("200",map);
    }
}
